﻿package system;

/*
 * Author :justin
 * CreateTime:2004-09-21 15:47:20 登陆上下文
 */

import com.hitrust.gs.lib.GSSystemConfigurator;

import system.base.User;
import system.base.UserMenuinfo;

import framework.core.*;
import system.base.Warehouse;
//import com.v3.user.*;

public class LoginContext implements ILoginContext
{
    
    private static final String LOGIN_SESSION_NAME = "LoginContext";

	private User _user = null;
	
	private Warehouse _warehouse = null;
   
    private boolean isDev = false;

    private boolean isView=false;
    
    private boolean isProject =false;
    
    
    /**
     * @return the isView
     */
    public boolean isView() {
        return this.isView;
    }

    /**
     * @param aIsView the isView to set
     */
    public void setView(boolean aIsView) {
        this.isView = aIsView;
    }

    public LoginContext()
    {
 
    }

	public User getUser() throws Exception
	{
		return _user;

	}

	public void setUser(User user)
	{
		_user = user;
	}
	
	public Warehouse getWarehouse() throws Exception
	{
		return _warehouse;

	}

	public void setWarehouse(Warehouse warehouse)
	{
		_warehouse = warehouse;
	}	

	public boolean isLogin()
	{
		return (_user != null);
	} 


    public ILoginContext init(SrvContext context)
    {
        LoginContext loginContext = null;
        loginContext = (LoginContext) context
                .getSessionValue(LOGIN_SESSION_NAME);
        if (loginContext == null)
        {
            loginContext = new LoginContext();
        }
        return loginContext;
    }
    
	public String getLoginUserName()
	{
		if (_user == null)
			return "";
		return _user.getName();
	}

	public String getLoginUserId()
	{
		if (_user == null)
			return "";
		return _user.getId();
	}   
	
	public String getLoginWarehouseName()
	{
		if (_warehouse == null)
			return "";
		return _warehouse.getName();
	}

	public String getLoginWarehouseCode()
	{
		if (_warehouse == null)
			return "";
		return _warehouse.getCode();
	} 
	

		   

    public static boolean login(DBConnection con, SrvContext context,
            String userCode, String password, boolean isDev) throws Exception
    {
        //CLEAN
        context.setSessionValue(LOGIN_SESSION_NAME, null);
//
//		//CHECK
		User user = User.fetch(con, userCode, password);
		if (user == null)
			return false; //bad userCode or password	
			
		Warehouse warehouse = Warehouse.fetchByCode(con,user.getWarehouseCode());		
		//是否有权限进行视图切换
		boolean bl=false;
		if(userCode.equals(ConstDefine.T_USER_SUPERADMIN_CODE)){
		    if ("true".equalsIgnoreCase(GSSystemConfigurator.getParam("gs.view.switch"))) {
		        bl=true;
            }
		}else{
    		ObjectList list=UserMenuinfo.fetchbyUserId(user.getId(), con);
            for (int i = 0; i < list.size(); i++) {
                UserMenuinfo um=(UserMenuinfo) list.get(i);
                if(um.getMenuCode()==ConstDefine.VIEW_SWITCH){
                    bl=true;
                    break;
                }
            }
		}
        //LOGIN
        LoginContext loginContext = new LoginContext();
        loginContext.setView(bl);
		loginContext.setUser(user);
		loginContext.setWarehouse(warehouse);
        loginContext.setDev(isDev);
        context.setSessionValue(LOGIN_SESSION_NAME, loginContext);
        return true;
    }

 

    public static void loginOut(SrvContext context) throws Exception
    {
        //清除登录信息
        context.setSessionValue(LOGIN_SESSION_NAME, null);
    }

    public static LoginContext get(SrvContext context)
    {
        LoginContext loginContext = null;
        loginContext = (LoginContext) context
                .getSessionValue(LOGIN_SESSION_NAME);
        if (loginContext == null)
        {
            loginContext = new LoginContext();
        }
        return loginContext;
    }

    public void checkLogin() throws Exception
    {
        if (isLogin())
            return;
        throw new Exception("login error!!");
    }

	public boolean isDev()
	{
		return isDev;
	}

	public void setDev(boolean isDev)
	{
		this.isDev = isDev;
	}

	public boolean isProject() {
		return isProject;
	}

	public void setProject(boolean isProject) {
		this.isProject = isProject;
	}
	
	
}